/*
 * Copyright 2009 Brian Mackay brianwmackay@msn.com
 *   All Rights Reserved. See end of file
 */
package com.bmackay.vonsteuben.base.conf;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;

public class ConfigParserImpl extends Configuration implements ConfigParser {

    public ConfigParserImpl(Configuration configuration) {
        super(configuration);
    }

    public ConfigParserImpl(String configPath ) {
        super();
        add(configPath);
    }

    public ConfigParserImpl(String configPath, boolean loadDefaults) {
        super(loadDefaults);
        add(configPath);
    }

    private void add(String configPath) {
        try {
            addResource(new Path(configPath));
        } catch (Exception e) {
            System.out.println("ConfigParserImpl:addResource:error" + e);
        }
    }

    public List<Map.Entry<String, String>> getAll() {

        List<Map.Entry<String, String>> list = new ArrayList();
        Iterator<Map.Entry<String, String>> iterator = iterator();
        for (Iterator<Map.Entry<String, String>> it = iterator; it.hasNext();) {
            Map.Entry<String, String> entry = it.next();
            list.add(entry);
        }
        return list;
    }

    @Override
    public String getValue(String name) {
        String value = get(name);
        if (value == null) {
            value = "";
        }
        return value.trim();
    }

    @Override
    public boolean hasValue(String name) {
        String value = get(name);
        if (value == null) {
            return false;
        }
        if (value.equals("")) {
            return false;
        }
        return true;
    }
}
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
